﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MiserBlogMVC.Models.DataHelper;
using MiserBlogData.Entities;
using MiserBlogMVC.Filter;
using MiserBlogHelper.Helper;
using MiserBlogMVC.Models;
using Iesi.Collections.Generic;

namespace MiserBlogMVC.Controllers
{
    [AuthorUserVIPFilter]
    public class ManageArticleController : Controller
    {
        public ActionResult Index()
        {

            UserVIPIdentity vipIdentity = (UserVIPIdentity)HttpContext.User.Identity;
            UserVIP vip = (UserVIP)vipIdentity.Person;
            ManageArticlePage managePage = new ManageArticlePage();
            HttpContextHelper contextHelper = new HttpContextHelper(this.HttpContext);

            vip.ArticleList = (List<Article>)managePage.GetPage<Article>(contextHelper);
            return View(vip);
        }

        [ValidateInput(false)]
        public ActionResult Create(int? id)
        {
            if (id.HasValue)
            {
                MiserDB.DataHelp.QueryHelper data = new MiserDB.DataHelp.QueryHelper();
                Article article = data.GetData<Article>(id.Value);

                foreach (var item in article.Tags)
                {
                    article.TagStr += item.Name + ",";
                }
                article.TagStr = StrHelper.CutEnd(article.TagStr);

                return View(article);
            }
            return View();
        }

        [ValidateInput(false)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(Article article, int? id)
        {
            UserVIPIdentity vipIdentity = (UserVIPIdentity)HttpContext.User.Identity;
            UserVIP vip = (UserVIP)vipIdentity.Person;
            article.User = vip;

            if (id.HasValue)
            {

                using (MiserDB.DataHelp.ActionTranHelper data = new MiserDB.DataHelp.ActionTranHelper())
                {

                    BuildTagForArticle(data, article, vip);
                    article.CDate = article.CDate;
                    article.UDate = DateTime.Now;
                    data.SaveOrUpdate<Article>(article);
                    return RedirectToRoute(new { action = "Index", controller = "ManageArticle" });
                }
            }
            else
            {

                using (MiserDB.DataHelp.ActionTranHelper data = new MiserDB.DataHelp.ActionTranHelper())
                {
                    ////得到这个用户所有的tag记录
                    BuildTagForArticle(data, article, vip);
                    article.CDate = DateTime.Now;
                    int newId = (int)data.Save<Article>(article);
                    return RedirectToRoute(new { action = "Index", controller = "ManageArticle" });
                }
            }
        }

        protected void BuildTagForArticle(MiserDB.DataHelp.ActionTranHelper data, Article article,UserVIP vip)
        {
            IList<Tag> tagList = data.GetSelfPage<Tag>(vip.Id, null);
            IList<Tag> articleTags = new List<Tag>();
            string[] tagAry = article.TagStr.Split(',');

            foreach (var intoTag in tagAry)
            {
                bool isExist = false;
                foreach (var nowTag in tagList)
                {
                    if (intoTag == nowTag.Name)
                    {
                        isExist = true;
                        articleTags.Add(nowTag);
                        break;
                    }
                }
                //不存在 向数据库插入数据
                if (isExist == false)
                {

                    Tag newTag = new Tag
                    {
                        Articles = new Iesi.Collections.Generic.HashedSet<Article>(),
                        Name = intoTag,
                        User = vip,
                        State = (int)DataSate.Start
                    };
                    int tagId = (int)data.Save<Tag>(newTag);
                    newTag.Id = tagId;
                    articleTags.Add(newTag);
                }
            }
            article.Tags = articleTags;
        }
    }
}
